package net.zjitc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Goods;
import net.zjitc.service.GoodService;
//import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//
@CrossOrigin
@RestController
public class GoodsController {
    @Autowired
    private GoodService goodService;
    //商品列表数据
    @GetMapping("goods")
    public ResponseResult getGood(@RequestParam(required = false) Integer query,
                                  @RequestParam(value ="pagenum", required = true) Integer pagenum,
                                  @RequestParam(value ="pagesize", required = true) Integer pagesize){
        ResponseResult result=new ResponseResult();
        Page<Goods> goods=goodService.selectPage(pagenum,pagesize);
        result.setSuccess("获取成功",goods);
        return result;
    }
    //添加商品
    @PostMapping("goods")
    public ResponseResult addGood(@RequestBody Goods goods){
        ResponseResult result=new ResponseResult();
        Goods addGood= goodService.addGood(goods);
        result.setCreateSuccess("创建商品成功",addGood);
        return result;

    }
    //根据id查询分类
    @GetMapping("goods/{id}")
    public ResponseResult findById(@PathVariable("id") int id){
        ResponseResult result=new ResponseResult();
        Goods good=goodService.findById(id);
        result.setSuccess("查找商品成功",good);
        return result;
    }
    //编辑修改
    @PutMapping("goods/{id}")
    public ResponseResult updateGood(@RequestBody Goods goods,@PathVariable("id") int id){
        ResponseResult result=new ResponseResult();
        Goods good= goodService.updateGood(goods,id);
        result.setCreateSuccess("修改商品成功",good);
        return  result;
    }
    @DeleteMapping("goods/{id}")
    public ResponseResult deleteGood(@PathVariable("id") int id){
        ResponseResult result=new ResponseResult();
        int num= goodService.deleteGood(id);
        result.setSuccess("删除成功");
        return result;
    }

}
